<template>
<table
    class="water-cp-container"
    cellpadding="0" cellspacing="0"
    border="1">
    <tr class="top-container">
        <td class="left">
            矿化度判识 基础数据
        </td>
        <td class="right">
            当前报告
        </td>
    </tr>
    <tr class="content-container">
        <td class="left">
            <div class="container">
                <div class="chart">
                    <MyChart
                        ref="MyChart">
                    </MyChart>
                </div>
                <div class="base-data">
                    <div class="base-data-title">
                        区域划分
                    </div>
                    <table border="1">
                        <tr>
                            <th>矿化度</th>
                            <th>天然水类型</th>
                        </tr>
                        <tr>
                            <td class="cols1">大于50g/L</td>
                            <td class="cols2">卤水</td>
                        </tr>
                        <tr>
                            <td class="cols1">介于10~50g/L</td>
                            <td class="cols2">盐水</td>
                        </tr>
                        <tr>
                            <td class="cols1">介于3~10g/L</td>
                            <td class="cols2">咸水</td>
                        </tr>
                        <tr>
                            <td class="cols1">介于1~3g/L</td>
                            <td class="cols2">微咸水</td>
                        </tr>
                        <tr>
                            <td class="cols1">小于1g/L</td>
                            <td class="cols2">淡水</td>
                        </tr>
                    </table>
                </div>
            </div>
        </td>
        <td class="right">
            <div class="chart">
                <MyChart
                    ref="MyChart1">
                </MyChart>
            </div>
        </td>
    </tr>
    <tr class="bottom-container" v-if="contrastResult">
        <td
            colspan="2"
            class="container">
            <span>所选报告矿化度判识为</span>
            <div class="data">{{contrastResult}}</div>
        </td>
    </tr>
</table>
</template>
<script>
/*
 矿化度判识展示页面
 */
import {randomColor} from "@/common/ColorTools";
import {computedRem} from "@/common/OtherTools";
import MyChart from '@/components/MyChart';
import {isEmptyObject} from "@/common/OtherTools";
export default {
    components:{MyChart},
    props:{
        currentReport:{  //当前选择的报告
            type:Object,
            default:()=>{
                return {};
            },
        },
        fontColor:{
            type:String,
            default:'',
        },
        borderColor:{
            type:String,
            default:'',
        },
        chartBorderColor:{
            type:String,
            default:'',
        },
    },
    data() {
        this.colorMap = {};  //颜色列表(保持颜色一致)
        this.values = [];  //所有值列表，用来计算数据上下限
        return {
            contrastResult:'',
        };
    },
    watch:{
        currentReport(){
            this.getReportData();
        },
    },
    mounted(){
        this.getBaseData();
    },
    methods:{
        randomColor,
        getBaseData(){  //获取基础数据
            this.$Service.post(
                this.$Api.judgment,
                {
                    "isbasic":1,
                    "type":1,
                },
            ).then(data => {
                let target = this.$refs.MyChart;
                for(let index in data.data){
                    let value = data.data[index];
                    value = value.map(item=>{
                        return item/1000;
                    });
                    this.values.push(...value);
                }
                this.drawChart({target,data:data.data});
            }).catch(() => {
                return;
            });
        },
        getReportData(){  //获取报告数据
            this.contrastResult = '';
            let target = this.$refs.MyChart1;
            if(isEmptyObject(this.currentReport)){
                this.drawChart({target,data:[]});
                return;
            }
            this.$Service.post(
                this.$Api.judgment,
                {
                    "reportType":this.currentReport.reportType,
                    "reportNo":this.currentReport.reportNo,
                    "type":1,
                },
            ).then(data => {
                this.contrastResult = this.onContrast(data.data[Object.keys(data.data)[0]]);
                this.drawChart({target,data:data.data});
            }).catch(() => {
                this.drawChart({target,data:[]});
                return;
            });
        },
        onContrast(data){  //比对
            switch(true){
                case data > 50:
                    return '卤水';
                case data > 10 && data<=50:
                    return '盐水';
                case data > 3 && data<=10:
                    return '咸水';
                case data >= 1 && data<=3:
                    return '微咸水';
                case data < 50:
                    return '淡水';
            }
        },
        drawChart({target,data={}}){  //绘制表格
            let data_ = [];
            for(let index in data){
                let value = data[index];
                value = value.map(item=>{
                    return item/1000;
                });
                data_.push({
                    name:index,
                    value:value,
                });
            }
            data = data_;
            let rem = computedRem();
            let color = this.fontColor;
            target.setOption({
                textStyle:{
                    fontSize:rem*1.5,
                    color:color
                },
                legend: {
                    show:true,
                    bottom:'bottom',
                    textStyle: {
                        color: color,  // 图例文字颜色
                        fontSize:rem*1,
                    },
                },
                dataset: [
                    ...data.map(item=>{
                        return {
                            source: [
                                item.value,
                            ],
                        };
                    }),
                    ...data.map((item,index)=>{
                        return {
                            fromDatasetIndex: index,
                            transform: {
                                type: 'boxplot',
                                config: { itemNameFormatter: 'expr {value}' }
                            }
                        };
                    }),
                ],
                tooltip: {
                    trigger: 'item',
                    formatter: params=>{
                        return `${params.seriesName}:${params.value[1]}`;
                    },
                },
                grid: {
                    left: '10%',
                    right: '5%',
                    top: '10%',
                    bottom:'20%',
                },
                xAxis: {
                    type: 'category',
                    nameGap: 30,
                    splitArea: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    },
                    axisLine: {
                        show:false,
                    },
                    axisLabel:{  //x轴的标签设置
                        show:false,
                    },
                },
                yAxis: {
                    type: 'value',
                    name: 'g/L',
                    min:0,
                    max:Math.ceil(Math.max(...this.values)),
                    nameTextStyle:{
                        fontWeight:'bold',
                        fontSize:1*rem,
                    },
                    splitArea: {
                        show: false,
                    },
                    axisLine: {
                        show:true,
                        lineStyle: {  //y轴样式
                            color:color,
                        },
                    },
                    splitLine: {//分割线配置
                        show:true,
                        lineStyle: {
                            color: "#b8dbff63",
                        }
                    },
                },
                series: [
                    ...data.map((item,index)=>{
                        return {
                            name: item.name,
                            type: 'boxplot',
                            datasetIndex:index + data.length,
                            color:this.colorMap[item.name],
                            itemStyle: {
                                color: '#ffffff54',
                            },
                        };
                    }),
                ]
            });
        },
    },
};
</script>
<style scoped lang="scss">
.water-cp-container{
    width: 100%;
    height: fit-content;
    text-align: left;
    color: var(--font-color);
    box-sizing: border-box;
    border-collapse: collapse;
    border-color: var(--border-color);
    border: 1px solid var(--border-color);
    table-layout:fixed;
    >tr{
        >td{
            &:last-child{
                width: 40%;
            }
        }
    }
    >.top-container{
        >td{
            font-size: 1.5rem;
            font-weight: bold;
            padding: 1rem;
            box-sizing: border-box;
            line-height: 1;
            box-sizing: border-box;
        }
    }
    >.content-container{
        >.left{
            >.container{
                display: grid;
                grid-template-columns: 1fr 20rem;
                >.chart{
                    width: 100%;
                    height: 30rem;
                }
                >.base-data{
                    padding: 1rem 1rem 1rem 0;
                    box-sizing: border-box;
                    width: 100%;
                    text-align: center;
                    display: grid;
                    grid-gap: 1rem;
                    height: fit-content;
                    >.base-data-title{
                        font-size: 1.5rem;
                    }
                    >table{
                        width: 100%;
                        height: fit-content;
                        border-collapse: collapse;
                        border-color: #0f4b78;
                        border: 1px solid #0f4b78;
                        td,th{
                            padding: 1rem 0.5rem;
                            text-align: center;
                            font-size: 1rem;
                            box-sizing: border-box;
                        }
                        th{
                            font-size: 1rem;
                            font-weight: bold;
                        }
                        .cols2,.cols4,.cols6{
                            width: 50%;
                        }
                    }
                }
            }
        }
        >.right{
            >.chart{
                width: 100%;
                height: 30rem;
            }
        }
    }
    >.bottom-container{
        >.container{
            padding: 1.5rem 1rem;
            font-size: 1.5rem;
            color: #99a9bf;
            box-sizing: border-box;
            span{
                font-size: 1.7rem;
                color: var(--font-color);
            }
            .data{
                font-size: 2rem;
                color: #00c57a;
                font-weight: bold;
            }
        }
    }
}
</style>
